package a02_链表;

/**
 * <p>
 * a06_删除链表的倒数第N个节点复习5
 * </p>
 *
 * @author flyduck
 * @since 2025/1/8
 */
public class a06_删除链表的倒数第N个节点复习5 {
    //a b c d e f   2
    //slow
    //      fast走3步
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummyNode = new ListNode(0);
        dummyNode.next = head;

        ListNode slowNode = dummyNode;
        ListNode fastNode = dummyNode;
        while (n + 1 > 0) {
            fastNode = fastNode.next;
            n--;
        }
        while (fastNode != null) {
            slowNode = slowNode.next;
            fastNode = fastNode.next;
        }
        slowNode.next = slowNode.next.next;
        return dummyNode.next;
    }
}
